{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "68587ff0-cc9d-4a4c-9ca0-df46c20839ba",
   "metadata": {},
   "source": [
    "Chapter 05\n",
    "\n",
    "# 成对欧氏距离\n",
    "Book_6《数据有道》 | 鸢尾花书：从加减乘除到机器学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "22d75b52-c37a-437d-8b74-3897be0e5536",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics.pairwise import euclidean_distances\n",
    "\n",
    "# Sample data points\n",
    "X = [[-5, 0], [4, 3], [3, -4]]\n",
    "\n",
    "# Query point\n",
    "q = [[0, 0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f6e4443f-b711-44d7-805e-e37071f2f83c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pairwise distances between X and q\n",
      "[[5.]\n",
      " [5.]\n",
      " [5.]]\n"
     ]
    }
   ],
   "source": [
    "# pairwise distances between rows of X and q\n",
    "dst_pairwise_X_q = euclidean_distances(X, q)\n",
    "print('Pairwise distances between X and q')\n",
    "print(dst_pairwise_X_q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d5b436f6-6a6b-42fc-85f0-2f4264125888",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pairwise distances between X and X\n",
      "[[0.         9.48683298 8.94427191]\n",
      " [9.48683298 0.         7.07106781]\n",
      " [8.94427191 7.07106781 0.        ]]\n"
     ]
    }
   ],
   "source": [
    "# pairwise distances between rows of X and itself\n",
    "dst_pairwise_X_X = euclidean_distances(X, X)\n",
    "print('Pairwise distances between X and X')\n",
    "print(dst_pairwise_X_X)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
